<template>
  <div>
    <div class="ser_div">
      <el-row>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 订单号 </el-col>
            <el-col :span="16">
              <el-input v-model="ser.order_number" type="number"></el-input>
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 流水号 </el-col>
            <el-col :span="16">
              <el-input v-model="ser.serial_number"></el-input>
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 客户 </el-col>
            <el-col :span="16">
              <el-select
                v-model="ser.customer_id"
                filterable
                placeholder="请选择客户"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in CustomerList"
                  :key="index"
                  :label="item.customer_name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 产品 </el-col>
            <el-col :span="16">
              <el-select
                v-model="ser.product_id"
                filterable
                @change="product_id_change"
                placeholder="请选择产品"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in productList"
                  :key="index"
                  :label="item.product_name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 型号 </el-col>
            <el-col :span="16">
              <el-select
              filterable
                v-model="ser.model_number_id"
                placeholder="请选择型号"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in ModelNumberList"
                  :key="index"
                  :label="item.model_number"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 状态 </el-col>
            <el-col :span="16">
              <el-select
                v-model="ser.logistics_status"
                placeholder="请选择物流状态"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in logistics_statuslist"
                  :key="index"
                  :label="item.lable"
                  :value="item.status"
                >
                </el-option>
              </el-select>
              
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-row style="display: flex; align-items: center">
            <el-col :span="8" style="text-align: center"> 订单日期范围 </el-col>
            <el-col :span="16">
              <el-date-picker
                v-model="order_data"
                @change="order_data_change"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-col>
          </el-row>
        </el-col>
        <el-col>
          <el-button @click="search(0)" class="add_btn" icon="el-icon-search"
            >查询</el-button
          >
          <el-button @click="searchreset" icon="el-icon-refresh" class="add_btn"
            >重置</el-button
          >
          <el-button @click="search(2)" icon="el-icon-plus" class="add_btn"
            >导出</el-button
          >
        </el-col>
      </el-row>
    </div>
    <el-table
      :data="dataList"
      stripe
      v-loading="loading"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      border
    >

      <el-table-column type="selection"> </el-table-column>
      <el-table-column prop="id" label="ID" width="60"> </el-table-column>
      <el-table-column prop="order_id" label="订单ID" width="80"> </el-table-column>

      <el-table-column prop="warehouse_entry_time" label="入库日期" width="100">
      </el-table-column>
      <el-table-column label="客户名称" width="80">
        <template slot-scope="scope">
          {{ scope.row.order.customer?scope.row.order.customer.customer_name :''}}
        </template>
      </el-table-column>
      <el-table-column label="订单号" width="120">
        <template slot-scope="scope">
          {{ scope.row.order.order_number }}
        </template>
      </el-table-column>
      <el-table-column label="流水号" width="120">
      <template slot-scope="scope">
          {{ scope.row.order.serial_number }}
        </template>
      </el-table-column>
      <el-table-column label="产品名称" width="100">
        <template slot-scope="scope">
          {{ scope.row.order.product_name.product_name }}
        </template>
      </el-table-column>
      <el-table-column label="产品型号" width="120">
        <template slot-scope="scope">
          {{ scope.row.order.model_number_name.model_number }}
        </template>
      </el-table-column>
      <el-table-column label="方向" width="90">
        <template slot-scope="scope">
          {{ scope.row.direction }}
        </template>
      </el-table-column>
      <el-table-column label="规格" width="100">
        <template slot-scope="scope">
          {{ scope.row.order.specification?scope.row.order.specification.specification:'' }}
        </template>
      </el-table-column>
      <el-table-column label="颜色" width="80">
        <template slot-scope="scope">
          {{ scope.row.order.color.color }}
        </template>
      </el-table-column>
      <el-table-column label="备注" width="120">
        <template slot-scope="scope">
          {{ scope.row.order.remark }}
        </template>
      </el-table-column>
      <el-table-column prop="logistics_status" label="发货状态" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.logistics_status == 1" style="color: #dd8211"
            >待发货</span
          >
          <span v-if="scope.row.logistics_status == 2" style="color: #f1420d"
            >未发货</span
          >
          <span v-if="scope.row.logistics_status == 3" style="color: #0ca163"
            >已发货</span
          >
          <span v-if="scope.row.logistics_status == 4" style="color: #c9be2f"
            >部分待发货</span
          >
          <span v-if="scope.row.logistics_status == 5" style="color: #c9be2f"
            >部分未发货</span>
        </template>
      </el-table-column>
      <el-table-column label="件数" width="80">
        <template slot-scope="scope">
          {{ scope.row.num}}
        </template>
      </el-table-column>

      <el-table-column label="已发件数" width="80">
        <template slot-scope="scope">
          {{ scope.row.sent_num}}
        </template>
      </el-table-column>
      <el-table-column label="待发件数" width="80">
        <template slot-scope="scope">
          {{ scope.row.to_be_sent_num}}
        </template>
      </el-table-column>
      <el-table-column label="仓库" width="80">
        <template slot-scope="scope">
          {{ scope.row.warehouse.warehouse_name}}
        </template>
      </el-table-column>
      
      <el-table-column prop="delivery_time"   :formatter="formatterTime" label="发货时间" width="120">
        <template slot-scope="scope">
          {{scope.row.delivery_time }}
        </template>
      </el-table-column>
      <el-table-column label="物流信息" width="120">
        <template slot-scope="scope">
          {{ scope.row.logistics_info }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" fixed="right">
        <template slot-scope="scope">
          <el-button 
          :disabled="scope.row.logistics_status==3"
            @click="fahuo_s(scope.row)" 
            type="text"
            >发货</el-button
          >
          <el-button  type="text" @click="chengpin_shanchu(scope.row.id,scope.row.order.order_number)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        @prev-click="prev"
        @next-click="next"
        :current-page.sync="pagination.page"
        :page-size="pagination.per_page"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog
      title="发货信息"
      :visible.sync="isjilu_lst"
      width="41%"
      @close="close_dialog"
    >
      <el-table
        :data="dataList_fahuo"
        stripe
        style="width: 100%"
        @selection-change="handleSelectionChange"
        border
      >
        <el-table-column label="已发件数" width="130">
          <template slot-scope="scope">
            {{ scope.row.sent_num }}
          </template>
        </el-table-column>
        <el-table-column label="待发发件数" width="130">
          <template slot-scope="scope">
            {{ scope.row.to_be_sent_num }}
          </template>
        </el-table-column>
        <el-table-column label="备注" width="243">
          <template slot-scope="scope">
            {{ scope.row.remark }}
          </template>
        </el-table-column>

        <el-table-column label="状态" width="120">
          <template slot-scope="scope">
            <span v-if=" scope.row.logistics_status==3"  style="color:#e6e21b">未发货</span>
            <span v-if=" scope.row.logistics_status==1" style="color:#038d58">自提</span>
            <span v-if=" scope.row.logistics_status==2" style="color:#038d58">物流</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              @click="fahuo(scope.row)"
              type="text"
              size="small"
              v-if="scope.row.logistics_status== 3"
              >发货</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isjilu_lst = false"  type="primary" size="small">取 消</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="发货信息"
      :visible.sync="isjilu"
      width="50%"
      @close="close_dialog"
    >
      <el-row style="display: flex; align-items: center; margin-top: 15px">
        <el-col :span="3"> 物流状态 </el-col>
        <el-col :span="21">
          <el-select
            v-model="fahuos.logistics_status"
            placeholder="请选择物流状态"
          >
            <el-option
              v-for="item in wuliulist"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-col>
      </el-row>
      <el-row style="display: flex; align-items: center; margin-top: 15px">
        <el-col :span="3"> 发货内容 </el-col>
        <el-col :span="21">
          <el-input type="text" v-model="fahuos.logistics_info"></el-input>
        </el-col>
      </el-row>
      <el-row style="display: flex; align-items: center; margin-top: 15px">
        <el-col :span="3"> 备注 </el-col>
        <el-col :span="21">
          <el-input type="text" v-model="fahuos.remark"></el-input>
        </el-col>
      </el-row>
      <el-row style="display: flex; align-items: center; margin-top: 15px">
        <el-col :span="3"> 短信提醒 </el-col>
        <el-col :span="21">
          <el-switch
            v-model="fahuos.duanxin"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isjilu = false" type="primary" size="small">取 消</el-button>
        <el-button @click="yes_fahuo" type="primary" size="small">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getFinishProductList,
  getStockAllocationList,
  stockAllocation,
  setDeliveryStatus,
  DeliverGoods,
  getCustomerList,
  getModelNumberList,
  getProductList,
  delFinishProduct,
  getFinishProductList_exports
} from "@/api/product/index";
export default {
  data() {
    return {
      loading:true,
      formatterTime(row, column) {
        let data = row[column.property];
        return /\d{4}-\d{1,2}-\d{1,2}/g.exec(data);
      },
      logistics_statuslist:[
        {
          status:1,
          lable:"待发货"
        },
        {
          status:2,
          lable:"未发货"
        },
        {
          status:3,
          lable:"已发货"
        },
        {
          status:4,
          lable:"部分待发货"
        },
        {
          status:5,
          lable:"部分未发货"
        }
       
      ],
      dataList_fahuo: [],
      isjilu_lst: false,
      // 搜索
      ModelNumberLists: [],
      ModelNumberList: [],
      order_data: [],
      ser: {
        serial_number:"",
        logistics_status:"",
        model_number_id: "",
        order_number: "",
        customer_id: "",
        product_id: "",
        order_date: "",
        is_replenishment: "",
        is_stock_up: "",
        start_time: "",
        end_time: "",
      },
      CustomerList: [],
      productList: [],
      wuliulist: [
        {
          label: "自提",
          value: 1,
        },
        {
          label: "物流",
          value: 2,
        }
      ],
      isdayin: false,
      isjilu: false,
      ishuojia: false,
      dataList: [],
      pagination: {
        per_page: 10,
        page: 1,
      },
      total: 0,
      dayinlist: [],
      diaoList: [],
      finish_product_id: "",
      diaohuojilulist: [],
      datalists: [],
      fahuos: {
        finish_product_id: "",
        order_logistics_id: "",
        logistics_status: "",
        remark: "",
        logistics_info: "",
        duanxin: false,
      },
    };
  },
  mounted() {
    this.getlist();
    this.getproduct();
    this.getModel();
    this.getCustomer();
  },
  methods: {
    del_FinishProduct(id){
      delFinishProduct({finish_product_id:id}).then(res=>{
          if(res.code==200){
            this.$message.success("操作成功~");
            this.getlist();
          }
      })
    },
    chengpin_shanchu(id,num){
      this.$confirm('您确定删除订单号为：'+num+'的订单吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
           this.del_FinishProduct(id)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 指定查询
    search(type) {
      if (type == 0) {
        this.pagination.per_page = 10;
        this.pagination.page = 1;
      }
      getFinishProductList({ ...this.ser, ...this.pagination }).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
          if (type == 2) {
            if (res.data.data.length > 0) {
              this.$message.success("正在导出订单列表");
              this.exports_w();
            }
          }
        }
      });
    },
    // 导出
    exports_w() {
      let data = {
        ...this.ser,
        export: 1,
      };
      getFinishProductList_exports(data).then((res) => {
        //创建一个隐藏的a连接，
        const link = document.createElement("a");
        let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
        link.style.display = "none";
        //设置连接
        link.href = URL.createObjectURL(blob);
        link.download = "物流列表";
        document.body.appendChild(link);
        //模拟点击事件
        link.click();
      });
    },
    searchreset() {
      this.ser = {
        model_number_id: "",
        order_number: "",
        customer_id: "",
        product_id: "",
        order_date: "",
        is_replenishment: "",
        is_stock_up: "",
        start_time: "",
        end_time: "",
      };
      this.getlist();
    },
    //客户列表
    getCustomer() {
      getCustomerList().then((res) => {
        if (res.code == 200) {
          this.CustomerList = res.data.data;
        }
      });
    },
    //产品列表
    getproduct() {
      getProductList().then((res) => {
        if (res.code == 200) {
          this.productList = res.data.data;
        }
      });
    },
    // 型号列表
    getModel() {
      getModelNumberList().then((res) => {
        if (res.code == 200) {
          this.ModelNumberLists = res.data.data;
        }
      });
    },
    order_data_change(val) {
      this.ser.start_time = val[0];
      this.ser.end_time = val[1];
    },
    product_id_change(val) {
      console.log(val);
      this.ser.model_number_id = "";
      this.get_producModel(val)
    },
    get_producModel(id) {
      getModelNumberList({ product_id: id,field_list:'id,model_number',is_dropdown:1 }).then((res) => {
        if (res.code == 200) {
          this.ModelNumberList = res.data.data;
        }
      });
    },
    push_daifahuo() {
      let ids = [];
      this.dayinlist.forEach((item) => {
        ids.push(item.id);
      });
      setDeliveryStatus({ ids }).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "操作成功",
          });
        }
      });
    },
    fahuo_s(row) {
      if(row.is_residual_payment==2){

        this.$message.error('此订单未收余款，不能发货！');
        return false

      }
      this.fahuos.finish_product_id = row.id;
      this.isjilu_lst = true;
      this.dataList_fahuo = row.order_logistics;
      console.log(this.dataList_fahuo)
    },
    fahuo(row) {
      this.fahuos.order_logistics_id = row.id;
      this.isjilu = true;
    },
    yes_fahuo() {
      DeliverGoods(this.fahuos).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "操作成功",
          });
          this.isjilu = false;
          this.isjilu_lst = false;
          this.getlist();
        }
      });
    },
    diaohuojilu(row) {
      this.isjilu = true;
      this.diaohuojilulist = row.stock_allocation_log;
    },
    close_dialog() {
      this.finish_product_id = "";
      this.diaohuojilulist = [];
    },
    //调货确认
    add_product(row) {
      stockAllocation({
        order_id: row.id,
        finish_product_id: this.finish_product_id,
      }).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "操作成功",
          });
          this.getlist();
          this.fahuos = {
            finish_product_id: "",
            order_logistics_id: "",
            logistics_status: "",
            remark: "",
            logistics_info: "",
            duanxin: false,
          };
          this.ishuojia = false;
        }
      });
    },
    diaohuo(row) {
      this.finish_product_id = row.id;
      getStockAllocationList({ id: row.id }).then((res) => {
        if (res.code == 200) {
          this.diaoList = res.data.data;
          this.ishuojia = true;
        }
      });
    },
    // 获取成品库列表
    getlist() {
      getFinishProductList({
        ...this.pagination,
        ...this.ser,
        order_type:"logistics",
      }).then((res) => {
        if (res.code == 200) {
          this.dataList = res.data.data;
          this.total = res.data.total;
          this.loading=false;
        }
      });
    },
    handleSelectionChange(val) {
      console.log(val);
      // this.dayinlist = val;
      this.datalists = val;
    },
    print() {
      this.$print(this.$refs.print);
      this.isdayin = true; // 2、绑定区域锚点print，进行打印
    },
    dayin() {
      this.$print(this.$refs.print);
    },
    handleSizeChange(val) {
      console.log(val);
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagination.page = val;
      this.getlist();
    },
    prev() {
      this.pagination.page = this.pagination.page - 1;
      this.getlist();
    },
    next() {
      console.log("ss");
      this.pagination.page = this.pagination.page + 1;
      this.getlist();
    },
  },
};
</script>

<style scoped>
#printArea {
  width: 100%;
  margin: 0 auto;
}

#printArea > div {
  margin-top: 50px;
}
#printArea > div:first-child {
  margin-top: 0px;
}
#printArea table {
  width: 100%;
  border-collapse: collapse;
}
#printArea table th {
  color: white;
  height: 50px;
  line-height: 50px;
  font-size: 35px;
}
#printArea table {
  border: 1px solid #c5c5c5;
}
#printArea table td,
table th {
  border: 1px solid #c5c5c5;
  text-align: center;
  padding: 0;
  margin: 0;
}

#printArea table tr td {
  height: 35px;
  line-height: 35px;
}
#printArea table thead th {
  background-color: #f4615c;
}

#printArea table tr th,
table tr td {
  padding: 5px 12px;
}

@page {
  size: auto; /* auto is the initial value */
  margin: 3mm; /* this affects the margin in the printer settings */
}
.procedure {
  word-wrap: break-word;
}
</style>
